<?php 
class Game_Model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function update_rank($player_id, $hasWon, $class = null){
        $data = '';
        if($hasWon == 1){
            $this->session->set_userdata('wins', $this->session->userdata('wins') + 1);
            $data = 'wins = wins + 1';
        }
        else if($hasWon == 0){
            $this->session->set_userdata('loss', $this->session->userdata('loss') + 1);
            $data = 'loss = loss + 1';
        }
        else if($hasWon == -1){
            $this->session->set_userdata('wins', $this->session->userdata('wins') - 1);
            $data = 'wins = wins - 1';
        }

        if($class != null){
            $data .= ', class = '.$class;
            $this->session->set_userdata('class', $class);
        }

        return $this->db->query("UPDATE users SET ".$data." WHERE id = ?", array($player_id));
    }

    function get_rank($player_id){
        return $this->db->select(array('wins', 'class'))
            ->from('users')
            ->where('id', $player_id)
            ->get()->result_array();
    }
}